<?php
/**
 * Created by PhpStorm.
 * User: xiaomeng
 * Date: 17-7-10
 * Time: 上午10:39
 */
namespace mphp\Socket;
use mphp\Core\syncDb;

class webSocket
{
    protected $ws;
    protected $config;

    public function __construct()
    {

    }
    public function run($config)
    {
        $this->config = $config;
        $this->config['process_name'] = isset($this->config['process_name'])?$this->config['process_name']:'swoole';
        $this->ws = new \swoole_websocket_server($this->config['host'], $this->config['port']);
        $this->ws->set([
            'worker_num' => isset($this->config['worker_num'])?$this->config['worker_num']:2,
            'max_request' => isset($this->config['max_request'])?$this->config['max_request']:5000,
            'task_worker_num' => isset($this->config['task_worker_num'])?$this->config['task_worker_num']:10,
            'task_max_request' => isset($this->config['task_max_request'])?$this->config['task_max_request']:10000,
            'backlog' => isset($this->config['backlog'])?$this->config['backlog']:128,
            'dispatch_mode' => isset($this->config['dispatch_mode'])?$this->config['dispatch_mode']:2,
            'log_file' => isset($this->config['log_file'])?$this->config['log_file']: __DIR__ . '/swoole.log',
            'log_level' => isset($this->config['log_level'])?$this->config['log_level']:5,
            'pid_file' => isset($this->config['pid_file'])?$this->config['pid_file']: __DIR__ . '/server.pid',
            'heartbeat_idle_time' => isset($this->config['heartbeat_idle_time'])?$this->config['heartbeat_idle_time']:600,
            'heartbeat_check_interval' => isset($this->config['heartbeat_check_interval'])?$this->config['heartbeat_check_interval']:60,
            'daemonize' => isset($this->config['daemonize'])?$this->config['daemonize']:1,
            'enable_reuse_port' => isset($this->config['enable_reuse_port'])?$this->config['enable_reuse_port']:true,
        ]);
        $this->ws->on('Start', array($this, 'onStart'));
        $this->ws->on('Shutdown', array($this, 'onShutdown'));
        $this->ws->on('ManagerStart', array($this, 'onManagerStart'));
        $this->ws->on('ManagerStop', array($this, 'onManagerStop'));
        $this->ws->on('WorkerStart', array($this, 'onWorkerStart'));
        $this->ws->on('WorkerStop', array($this, 'onWorkerStop'));
        $this->ws->on('Task',array($this,'onTask'));
        $this->ws->on('Finish',array($this,'onFinish'));
        $this->ws->on('Open',array($this,'onOpen'));
        $this->ws->on('Message',array($this,'onMessage'));
        $this->ws->on('Close',array($this,'onClose'));
        $this->ws->start();

    }
    public function onStart($server)
    {
        $this->set_process_name($this->config['process_name'],'master');
    }
    public function onShutdown($server)
    {

    }
    public function onManagerStart($server)
    {
        $this->set_process_name($this->config['process_name'],'manager');
    }
    public function onManagerStop($server)
    {
        syncDb::redis()->del('chat_online');
    }
    public function onWorkerStart($server,$worker_id)
    {
        $this->set_process_name($this->config['process_name'],'woeker'.'#'.$worker_id);
    }
    public function onWorkerStop($server,$worker_id)
    {
        echo "worker stop".$worker_id.PHP_EOL;
    }
    public function onTask($server, $task_id,$src_worker_id,$data)
    {
        Task::rTask($server, $task_id,$src_worker_id,$data);
    }

    public function onFinish($server, $task_id,$data)
    {
        Finish::finish($server, $task_id,$data);
    }
    public function onOpen($server, $request)
    {
        echo "server:handshake success with fd{$request->fd}" . PHP_EOL;
    }

    public function onMessage($server, $frame)
    {
        Message::handleMessage($server, $frame);

    }

    public function onClose($server, $fd)
    {
        $online = syncDb::redis()->get('chat_online');
        $online = json_decode($online,true);
        $user =array_search($fd,$online);
        unset($online[$user]);
        $online = json_encode($online,JSON_UNESCAPED_UNICODE);
        syncDb::redis()->set('chat_online',$online);
    }

    public function set_process_name($process_name,$add_name)
    {
        swoole_set_process_name($process_name.'_'.$add_name);
    }


}